﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

namespace QuanLiDiemDanh.Data
{
    public class AccessData
    {
        private string ConnectString = ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
        //lấy chuỗi kết nối
        public SqlConnection GetConnect()
        {
            return new SqlConnection(ConnectString);
        }
        //hàm trả về 1 datatable
        public DataTable GetTable(string sql)
        {
            SqlConnection con = GetConnect();
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            con.Close();
            return dt;
        }
        //hàm thực thi lệnh executenonquery
        public void ExeCuteNonquery(string sql)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            cmd.Dispose();
        }
        //Hàm thực thi lệnh ExecuteScalar để trả về 1 giá trị
        public string ExecuteScalar(string sql)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            string kq = cmd.ExecuteScalar().ToString();
            con.Close();
            cmd.Dispose();
            return kq;
        }
        public SqlDataReader ExecuteReader(string sql)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        public string valid(OleDbDataReader myreader, int stval)
        //if any columns are found null then they are replaced by zero
        {
            object val = myreader[stval];
            if (val != DBNull.Value)
                return val.ToString();
            else
                return Convert.ToString(null);
        }

    }
}